import 'package:flutter/material.dart';

class InputWidget extends StatelessWidget {
  const InputWidget({ super.key });

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Handle Input"),
        centerTitle: true,
      ),
      body: Container(
        color: Colors.cyan.withOpacity(0.2),
        child: const Center(
          child: FormWidget(),
        ),
      ),
    );
  }
}

class FormWidget extends StatefulWidget {
  const FormWidget({ super.key });

  @override
  State<FormWidget> createState() => _FormWidget();
}

class _FormWidget extends State<FormWidget> {
  final myController = TextEditingController();
  var myText = "";

  @override
  void dispose() {
    myController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        TextField(controller: myController),
        
        Container(
          padding: const EdgeInsets.only(top: 16.0),
          child: FloatingActionButton(
            onPressed: () {
              showDialog(
                context: context,
                builder: (context) {
                  return AlertDialog(
                    content: Text(myController.text),
                  );
                }
              );
            },
            tooltip: "点我查看文字",
            child: const Icon(Icons.text_fields),
          ),
        ),

        TextField(
          onChanged: (text) {
            myText = text;
            print(text);
          },
        ),
        
        Container(
          padding: const EdgeInsets.only(top: 16.0),
          child: Text(myText),
        ),
      ],
    );
  }

}